<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="outDialog">
    <el-form :model="empForm" :rules="rules" ref="form">
      <el-form-item label="姓名:" label-width="120px" prop="username">
        <el-input
          v-model="empForm.username"
          autocomplete="off"
          style="width:500px"
          placeholder="请输入姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机:" label-width="120px" prop="mobile">
        <el-input
          v-model="empForm.mobile"
          autocomplete="off"
          style="width:500px"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <el-form-item label="入职时间:" label-width="120px" prop="timeOfEntry">
        <div class="block">
          <el-date-picker
            v-model="empForm.timeOfEntry"
            type="date"
            placeholder="选择日期:"
            style="width:500px"
          >
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item
        label="聘用形式:"
        label-width="120px"
        prop="formOfEmployment"
      >
        <el-select
          v-model="empForm.formOfEmployment"
          placeholder="请选择"
          style="width:500px"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="工号:"
        label-width="120px"
        style="width:500px"
        prop="workNumber"
      >
        <el-input
          v-model="empForm.workNumber"
          autocomplete="off"
          style="width:500px"
          placeholder="1-20个字符"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门:" label-width="120px" prop="departmentName">
        <el-input
          v-model="empForm.departmentName"
          placeholder="请选择"
          style="width:500px"
          @focus="companyList"
          @blur="showTree"
        >
        </el-input>
        <el-tree
          default-expand-all
          v-if="treeShow"
          v-loading="treeLoading"
          :data="treeData"
          :props="defaultProps"
          @node-click="handleNodeClick"
        ></el-tree>
      </el-form-item>
      <el-form-item label="转正时间:" label-width="120px" prop="correctionTime">
        <div class="block">
          <el-date-picker
            v-model="empForm.correctionTime"
            type="date"
            placeholder="选择日期"
            style="width:500px"
          >
          </el-date-picker>
        </div>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="outDialog">取 消</el-button>
      <el-button type="primary" @click="sevn">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
//导入新增员工接口
import { addStaff } from '@/api/employees'
// 导入获取部门列表
import { getCompany } from '@/api/department'
// 导入递归组件
import { getDeptList } from '@/utils/index'
// 导入枚举
import enumObj from '@/api/constant/employees'
//  导入过滤器
import { formatDate } from '@/filters'
export default {
  name: 'addEmp',
  props: {
    showDialog: {
      title: Boolean,
      required: true
    }
  },
  data () {
    return {
      //枚举数据
      hireType: enumObj.hireType,
      // 控制树形控件是否显示的属性
      treeShow: false,
      //加载动画效果
      treeLoading: false,
      // 树形控件绑定数据
      treeData: [],
      // 表单数据
      empForm: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      //校验规则,
      rules: {
        username: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '请选择', trigger: 'blur' }],
        formOfEmployment: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '请输入工号', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请选择', trigger: 'chang' }
        ],
        correctionTime: [{ required: true, message: '请选择', trigger: 'blur' }]
      },
      // 指定树的文字和子节点是什么属性
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  methods: {
    // 参数1：data被点的这个节点的数据
    // 参数2: node被点的这个节点对象
    // 参数3: tree被点的这个节点所在的tree组件对象
    handleNodeClick (data) {
      this.empForm.departmentName = data.name
      // console.log(data, node, tree)
      this.treeShow = false
    },

    //获得焦点事件
    // 思路：先加载树形结构，然后开启加载动画，发请求拿取部门树形数据，
    //利用之前封装好的递归函数，把返回来的数据变成树形结构，数据加载完关闭加载动画
    async companyList () {
      this.treeShow = true
      this.treeLoading = true
      let res = await getCompany()
      this.treeData = getDeptList(res.data.depts, '')
      this.treeLoading = false
    },

    // 失去焦点事件
    //思路：通过e的事件对象里面relatedTarget.className来判断
    showTree (e) {
      if (e.relatedTarget.className.indexOf('el-tree-node') == -1) {
        this.treeShow = false
      }
    },

    //新增确认按钮事件
    async sevn () {
      //过滤时间
      let newCorrectionTime = (this.empForm.correctionTime = formatDate(
        this.empForm.correctionTime
      ))
      //过滤时间
      let newtimeOfEntry = (this.empForm.timeOfEntry = formatDate(
        this.empForm.correctionTime
      ))
      // 发请求添加员工
      try {
        await addStaff({
          ...this.empForm,
          correctionTime: newCorrectionTime,
          timeOfEntry: newtimeOfEntry
        })
        this.$parent.showDialog = false
      } catch {
        this.$message.error('请输入正确的手机号')
      }
    },

    //新增取消按钮
    outDialog () {
      this.empForm = {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      }
      this.$refs.form.resetFields()
      //通知父组件关闭弹窗
      this.$parent.showDialog = false
    }
  }
}
</script>

<style></style>
